﻿//Wrapper
var robots = {};
robots.base = "9er";
//Array
robots.models = [];
//Constructor
robots.Model = function (name, metal, picture) {
    this.name = name;
    this.metal = metal;
    this.picture = picture;
}
//Prototype
robots.Model.prototype.isOutdated = false;
robots.urlHelper = function (base) {
    var url = "https://" + base + ".firebaseio.com/"
    for (var i = 1; i < arguments.length; i++) {
        url += arguments[i] + "/"
    }
    return url + ".json";
}
//Sort
robots.sort = function () {
   robots.models.sort(function (a, b) {
        var nameA = a.name.toLowerCase(), nameB = b.name.toLowerCase()
        if (nameA < nameB) //sort string ascending
            return -1
        if (nameA > nameB)
            return 1
        return 0 //default return value (no sorting)
   });
    robots.drawTable();
};
//Add robots
robots.add = function () {
    var name, metal, picture;
    name = document.getElementById("name");
    metal = document.getElementById("metal");
    picture = document.getElementById("picture");
    var newRobot = new robots.Model(name.value, metal.value, picture.value);
    robots.models.push(newRobot);
    name.value = "";
    //metal.value = "";
    picture.value = "";
    robots.drawTable();
    robots.sendRobots(newRobot);
};
robots.drawTable = function () {
    var tableH = "<div>";
    for (var i in robots.models) {
        var newRobot = robots.models[i];
        tableH += "<div class=col-md-4>"
        tableH += "<h1>";
        tableH += "<i class='" + robots.getIcon(newRobot.metal) + "'></i>";
        tableH += newRobot.name + "</h1>";
        tableH += "<img src='" + newRobot.picture + "' class='img-responsive img-thumbnail'/><br/>";
        tableH += "<div class='btn btn-danger fa fa-recycle' onclick='robots.delete(" + i + ")'></div>";
        tableH += "<div class='btn btn-warning fa fa-edit' onclick='robots.showEdit(" + i + ")'></div>"
        tableH += "</div>";
    }
    tableH += "</div>";
    tableH += "</div>";
    document.getElementById("tableHolder").innerHTML = tableH;
};
//Icons from fontawesome
robots.getIcon = function (metal) {
    if (metal === "adamantium") {
        return "fa fa-star-half-o"
    }else if (metal === "vibranium") {
        return "fa fa-star"
    } else {
        return "fa fa-adjust"
    }
};

//Show Edit 
robots.showEdit = function (index) {
    var robo = robots.models[index];
    document.getElementById("editTitle").innerHTML = "Robot information " + robo.name;
    document.getElementById("editName").value = robo.name;
    document.getElementById("editMetal").value = robo.metal;
    document.getElementById("editPicture").value = robo.picture;
    document.getElementById("isOutdated").value = robo.isOutdated.toString();
    document.getElementById("saveChangesBtn").onclick = function () { robots.saveEdit(index); };
    $("#editModal").modal();

};
//Save Edit 
robots.saveEdit = function (index) {
    var robo = robots.models[index];
    robo.name = document.getElementById("editName").value;
    robo.metal = document.getElementById("editMetal").value;
    robo.picture = document.getElementById("editPicture").value;
    if (document.getElementById("isOutdated").value === "true") {
        robo.isOutdated = true;
    }
    else {
        delete robo.isOutdated;
    }
    $("#editModal").modal("hide");
    robots.drawTable();
};
robots.sendRobots = function (robo) {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "https://9er.firebaseio.com/models/.json");
    xhr.onload = function () {
        if (xhr.status >= 200 && xhr.status < 400) {
            var data = JSON.parse(xhr.response)
            robo.key = data.name;
        }
        else {
            console.log("Error on: " + xhr.status);
        }
    xhr.onerror = function () {
        alert("Comm Error...resending in 15s");
         setTimeout(sendHeroes, 15000);
    };
    xhr.send(JSON.stringify(robo));
};
robots.getRobots = function () {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://9er.firebaseio.com/models/.json", true);
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            console.log(this.response);
           // setTimeout(callback, 3000);
        }
        else {
            console.log(this.response);
        }
        var data = JSON.parse(this.response);
        for (var x in data) {
            data[x].__proto__ = robots.models.prototype;
            data[x].key = x;
            robots.models.push(data[x]);
        }
        robots.drawTable();
    };
    xhr.onerror = function () {
        console.log("Comm Error...resending in 15s");
        setTimeout(callback, 15000);
    };
    xhr.send(JSON.stringify());
};
//Delete Robot
robots.delete = function (index) {
    var xhr = new XMLHttpRequest();
    xhr.open("DELETE", robots.urlHelper(robots.base, "models", robots.models[index].key));
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            robots.getRobots();
        }
    };
    xhr.error = function () {
        console.log("Error on DELETE");
    }
    xhr.send();
robots.models.splice(index, 1);
robots.drawTable();
};
/************************* AJAX ***********************/
//Seeder
robots.seed = function () {
    robots.models.push(new robots.Model("Ultron", "adamantium", "http://upload.wikimedia.org/wikipedia/en/4/4f/Ultron2013.jpg"));
    robots.models.push(new robots.Model("Bender", "Other", "http://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png"));
    robots.models.push(new robots.Model("Gundam", "Other", "http://img4.wikia.nocookie.net/__cb20120617132407/gundam/images/5/5a/D-gundam-third.png"));
    robots.models.push(new robots.Model("Irobot", "vibranium", "http://www.androidguys.com/wp-content/uploads/2011/11/irobot-glance.jpg"));
    robots.models.push(new robots.Model("Android", "Other", "http://blog.appliedis.com/wp-content/uploads/2013/11/android1.png"))
    robots.drawTable();
};
//Clear All
robots.clearAll = function () {
    robots.models = [];
    robots.drawTable();
};
robots.seed();
robots.getRobots();